package sketch.seg.cd.exp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import sketch.doc.Sketch;
import sketch.ink.SampledPoint;
import sketch.ink.Stroke;
import sketch.seg.cd.CDStaticKH;

public class CDRhoK {

	public static final int MIN_K = 1;

	public static final int MAX_K = 20;

	public static final int GAP_K = 3;

	private double[][] rho;

	private Stroke sample;

	public CDRhoK() {

	}

	public void setSample(Stroke s) {
		sample = s;
	}

	public Stroke getSample() {
		return sample;
	}

	public void calculateRho() {
		SampledPoint[] ps = new SampledPoint[sample.getPoints().size()];
		sample.getPoints().toArray(ps);

		rho = new double[MAX_K][];

		for (int i = MIN_K; i < MAX_K; i += GAP_K) {
			rho[i] = new double[ps.length];

			for (int j = 0; j < ps.length; j++) {
				rho[i][j] = CDStaticKH.relativeRho(ps, j, i);
			}
		}
	}

	public void printResult(OutputStreamWriter out) {
		try {
			out.write("# ");
			for (int i = MIN_K; i < MAX_K; i += GAP_K) {
				out.write("k=" + i + " ");
			}
			out.write("\n\n");

			for (int i = 0; i < sample.getPoints().size(); i++) {
				out.write(i + " ");

				for (int j = MIN_K; j < MAX_K; j += GAP_K) {
					out.write(rho[j][i] + " ");
				}

				out.write("\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void printFile(File file) {
		try {
			printResult(new OutputStreamWriter(new FileOutputStream(file)));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Sketch sketch = new Sketch();
		sketch.readFromFile(new File("rho-k.sample"));

		CDRhoK cdrk = new CDRhoK();
		cdrk.setSample(sketch.getStrokeById(0));
		cdrk.calculateRho();
		cdrk.printFile(new File("rho-k.dat"));
	}
}
